package com.baor.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.File;


@Component
public class UploadUtil {//通用
    private final Integer min = 100000000;

    private final Integer max = 888888888;

    public List<String> uploads(List<MultipartFile> files, String filepath){
        List<String> paths = new ArrayList<>();
        //项目根路径
        String rootPath = System.getProperty("user.dir");
        for(MultipartFile file : files){
            String fileName = file.getOriginalFilename();
            String fileType = fileName.substring(fileName.indexOf("."));
            fileName = ((int)Math.ceil(Math.random()*(max-min)+min))+fileType;
            File newFile = new File(filepath+fileName);
            if(!newFile.exists()){
                newFile.mkdirs();
            }
            try {
                file.transferTo(newFile);
                paths.add(filepath+fileName);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return paths;
    }

}
